package com.sita.tingterest.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.sita.bike.R;
import com.sita.tingterest.Implements.GetEvalueJsonData;
import com.sita.tingterest.adapter.EvalueListAdapter;
import com.sita.tingterest.pojo.Evalue;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

public class EvalueFragment extends Fragment implements View.OnClickListener {
    public ListView mEvalue_List;
    public Button mBtn_Submit;
    public EditText mEdit_Evalue;
    public Evalue mEvalue;
    public String mMessage;
    public String mAvatar;
    public String mNickName;
    public ArrayList<Evalue> mList;
    public EvalueListAdapter evalueListAdapter;
    public ArrayList<Evalue> mEvalueArrayList;

    public EvalueFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_evalue, container, false);
        initView(view);
        initData();
        return view;
    }

    public void initView(View view) {
        mBtn_Submit = (Button) view.findViewById(R.id.btn_sendMessage);
        mEdit_Evalue = (EditText) view.findViewById(R.id.edit_message);
        mEvalue_List = (ListView) view.findViewById(R.id.evalue_listView);
        mList = new ArrayList<Evalue>();
        mBtn_Submit.setOnClickListener(this);
    }

    public void initData() {
        DownLoadDatas downLoadDatas = (DownLoadDatas) new DownLoadDatas(new GetEvalueJsonData() {
            @Override
            public void getEvalueData(String data) {

                evalueListAdapter = new EvalueListAdapter(getActivity(), getJsonData(data));
                mEvalue_List.setAdapter(evalueListAdapter);
            }
        }).execute("http://www.sitayun.com:8080/tinterest/v1/notes/list");

    }

    public ArrayList<Evalue> getJsonData(String content) {
        JSONObject jsonObject = null;
        JSONObject jsonObject_data = null;
        try {
            jsonObject = new JSONObject(content);
            jsonObject_data = jsonObject.getJSONObject("data");
            JSONArray jsonArray_notes = jsonObject_data.getJSONArray("notes");
            for (int i = 0; i < jsonArray_notes.length(); i++) {
                mEvalue = new Evalue();
                JSONObject jsonObject_message = jsonArray_notes.getJSONObject(i);
                mMessage = jsonObject_message.getString("message");
                mEvalue.setMessage(mMessage);
                JSONObject jsonObject_account = jsonObject_message.getJSONObject("account");
                mNickName = jsonObject_account.getString("nickName");
                mAvatar = jsonObject_account.getString("avatar");
                mEvalue.setAvatar(mAvatar);
                mEvalue.setNickName(mNickName);
                mList.add(mEvalue);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return mList;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_sendMessage:
                String url = "http://www.sitayun.com:8080/tinterest/v1/notes/new";
                String Messge = mEdit_Evalue.getText().toString();
                submitPostData(url, Messge);
                mList.clear();
                break;
        }
    }

    public static String submitPostData(String url, String message) {
        String result = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            //添加http头信息
            httppost.addHeader("Accept", "application/json"); //认证token
            httppost.addHeader("Content-Type", "application/json");
            //http post的json数据格式：  {"name": "your name","parentId": "id_of_parent"}
            JSONObject obj = new JSONObject();
            obj.put("accountId", "129");
            obj.put("resId", "243783034665436188");
            obj.put("message", message);
            httppost.setEntity(new StringEntity(obj.toString()));
            HttpResponse response;
            response = httpclient.execute(httppost);
            //检验状态码，如果成功接收数据
            int code = response.getStatusLine().getStatusCode();
            if (code == 200) {
                result = EntityUtils.toString(response.getEntity());
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
            Log.i("sd", "" + e);
        } catch (Exception e) {
            Log.i("ss", "" + e);
        }
        return result;
    }

    public static String submitRequestData(String url) {
        String result = "";
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            //添加http头信息
            httppost.addHeader("Accept", "application/json"); //认证token
            httppost.addHeader("Content-Type", "application/json");
            //http post的json数据格式：  {"name": "your name","parentId": "id_of_parent"}
            JSONObject obj = new JSONObject();
            obj.put("resId", "243783034665436188");
            httppost.setEntity(new StringEntity(obj.toString()));
            HttpResponse response;
            response = httpclient.execute(httppost);
            //检验状态码，如果成功接收数据
            int code = response.getStatusLine().getStatusCode();
            if (code == 200) {
                result = EntityUtils.toString(response.getEntity());
            }
        } catch (ClientProtocolException e) {
        } catch (IOException e) {
            Log.i("sd", "" + e);
        } catch (Exception e) {
            Log.i("ss", "" + e);
        }
        return result;
    }

    class DownLoadDatas extends AsyncTask<String, Integer, String> {
        private GetEvalueJsonData getEvalueJsonDatas;

        public DownLoadDatas() {
        }

        public DownLoadDatas(GetEvalueJsonData getEvalueJsonData) {
            getEvalueJsonDatas = getEvalueJsonData;
        }

        @Override
        protected String doInBackground(String... params) {
            String content = submitRequestData(params[0]);
            return content;
        }

        @Override
        protected void onPostExecute(String s) {
            getEvalueJsonDatas.getEvalueData(s);

        }
    }
}
